//
//  CalculatorViewController.m
//  Calculator
//
//  Created by MildC on 11-5-14.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "CalculatorViewController.h"

@implementation CalculatorViewController

- (CalculatorBrain *)brain{
	if(!brain){
		brain = [[CalculatorBrain alloc] init];
	}
	return brain;
}

- (IBAction)digitPressed:(UIButton *)sender{
	NSString *digit = [[sender titleLabel] text];
	if ([digit isEqual:@"."]){
		userIsTypingFloatingNumber = YES;
		if ([[display text] doubleValue]==0){
			digit = @"0.";
		}
	}
	if (userIsInTheMiddleOfTypingANumber) {
		if (userIsTypingFloatingNumber && [[display text] doubleValue] - (int)[[display text] doubleValue]!=0){
			digit = @"";
		}
		[display setText:[[display text] stringByAppendingString:digit]];
	} else {
		[display setText:digit];
		userIsInTheMiddleOfTypingANumber = YES;
	}
}

- (IBAction)operationPressed:(UIButton *)sender{
	if (userIsInTheMiddleOfTypingANumber) {
		[[self brain] setOperand:[[display text] doubleValue]];
		userIsInTheMiddleOfTypingANumber = NO;
		userIsTypingFloatingNumber = NO;
	}
	NSString *operation = [[sender titleLabel] text];
	double result = [[self brain] performOperation:operation];
	[display setText:[NSString stringWithFormat:@"%g",result]];
}


@end

